<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/*
 * @param string $url 请求地址
 * @parma string httpType http请求类型
 * @parma array post_data post数据
 * @parma int timeout 等待时间
 * @parma data_type 数据类型
 * */
function curl($url, $httpType = 'get', $post_data = [], $timeout = 5, $header = "", $data_type = "") {
    //支持json数据数据提交
    if($data_type == 'json'){
        $post_string = json_encode($post_data);
    }
    else if(is_array($post_data)){
        $post_string = http_build_query($post_data, '', '&');
    }else {
        $post_string = $post_data;
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if($httpType == 'post')
        curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //不做验证，在Linux下改成true
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    //超时时间
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    //http请求Header
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
    //END
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

function getRandChars($length){
    $str = null;
    $strPol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $max = strlen($strPol) - 1;
    for ($i = 0;
         $i < $length;
         $i++){
        $str .= $strPol[rand(0,$max)];
    }
    return $str;
}